package com.trkj.service.wdm;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.OrganizationDAO;
import com.trkj.pojo.Organization;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor=Exception.class)
public class OrganizationService {
	@Autowired
	private OrganizationDAO organizationdao;
	
	/**
	 * 分页查询所有
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Organization> queryByPage(int page,int rows){
		return organizationdao.queryByNaPage(new PageRequest(page-1, rows,Direction.DESC,"organiEndtime"));
	}
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long queryByCount(){
		return organizationdao.count();
	}
	/**
	 * 新增
	 */
	public void insert(String organiName,String organiRemarks,String organiPerson){
		try {
			Organization organ = new Organization();
			organ.setOrganiName(organiName);
			organ.setOrganiRemarks(organiRemarks);
			organ.setOrganiPerson(organiPerson);
			Date date =new Date();
			organ.setOrganiTime(new Timestamp(date.getTime()));
			organ.setOrganiEndtime(new Timestamp(date.getTime()));
			organizationdao.save(organ);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 编辑
	 */
	public void update(Long id,String organiName,String organiPerson,String organiRemarks){
		try {
			Organization organ =organizationdao.findOne(id);
			organ.setOrganiName(organiName);
			organ.setOrganiPerson(organiPerson);
			organ.setOrganiRemarks(organiRemarks);
			organ.setOrganiEndtime(new Timestamp(new Date().getTime()));
			organizationdao.save(organ);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载
	 */
	
	public Organization load(Long organiId){
		return organizationdao.findOne(organiId);
	}
	
	
	/**
	 * 删除
	 */
	public void delete(Long organiId){
		try {
			organizationdao.delete(organiId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 模糊查询
	 * @param text
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Organization> selectForid_name(String text,int page,int rows){
		try {
			return organizationdao.query_By_idorname("%"+text+"%",new PageRequest(page-1, rows));
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long count(String text){
		return organizationdao.count("%"+text+"%");
	}
}
